-- UIDiary
-- Created by limj Mar/10/2015
-- 日记界面

UIDiary = class("UIDiary", function()
    return cc.Layer:create();
end);

function UIDiary.create(classId)
    return UIDiary.new(classId);
end

-- 元素图标路径
local eleIcon = {
    [ELEMENT_TYPE_EARTH] = "images/ui/diary/ele_earth.png",
    [ELEMENT_TYPE_WATER] = "images/ui/diary/ele_water.png",
    [ELEMENT_TYPE_FIRE]  = "images/ui/diary/ele_fire.png",
    [ELEMENT_TYPE_AIR]   = "images/ui/diary/ele_air.png", };

-- 构造函数
function UIDiary:ctor(classId)
    self:setName("UIDiary");
    local node = cc.CSLoader:createNode("layout/equip/Diary.csb");
    self:addChild(node);
    self.node = node;
    self:setLocalZOrder(UIMgr.TOP_MENU_ZORDER + 1);

    self.eleArr = {};
    self.allEle = {};

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘界面
    self:redraw(classId);

    -- 适配
    self:resize();
end

-- 注册点击事件
function UIDiary:registerTouchEvent()
    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIDiary");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));
end

-- 重绘
function UIDiary:redraw(classId)
    -- 标题
    local titleLabel = findChildByName(self.node, "CT/title_label");
    TextStyleM.setTextStyle(titleLabel, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_ORANGE, true);
    titleLabel:setString(getLocStr("diary_name"));

    -- 日期
    local dateLabel = findChildByName(self.node, "CT/date_label");
    local lastKeepDairy = ME.user.dbase:query("last_keep_diary") - TimeM.getTimeOffsetDifference();
    TextStyleM.setTextStyle(dateLabel, TextStyleM.TEXT_SIZE_SMALL2, TextStyleM.TEXT_COLOR_DARK_BROWN);
    dateLabel:setString(os.date("%b. %d", lastKeepDairy));

    -- 说明内容
    local descLabel = findChildByName(self.node, "CT/desc");
    local desc = ItemM.getItemDesc(classId);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    descLabel:setString(desc);

    -- 描述内容
    local detailLabel = findChildByName(self.node, "CT/detail");
    local detail = ItemM.getItemDetail(classId);
    TextStyleM.setTextStyle(detailLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE);
    detailLabel:setString(detail);

    -- 记录的元素组合
    -- 计算出滚动总高度
    local scrollview = findChildByName(self.node, "CT/ele_record");
    scrollview:removeAllChildren();
    scrollview:jumpToTop();

    self.allEle  = ElementAltarM.getAllConfig();

    local itemNode = cc.CSLoader:createNode("layout/equip/DiaryItem.csb");

    self.eleArr = table.keys(self.allEle);
    table.sort(self.eleArr);

    -- 图标间距(横向)
    local wGap = 0;

    -- 图标间距(纵向)
    local hGap = 3;

    -- 格子宽度
    local itemWidth = 0;

    -- 格子高度
    local itemHeight = 30;

    -- 左边距
    local leftWGap = 15;

    -- 行间距
    local topGap = 20;
    local bottomHGap = 0;

    -- 单页最大可容纳列数
    local MIN_GRID_COLUMN = 1;

    -- 获取记录总数
    local totalNum = #self.eleArr;

    -- 初始化滚动条
    ScrollTool.initScrollGridsVertical(self, scrollview, itemNode, totalNum, MIN_GRID_COLUMN, wGap, hGap, itemWidth, itemHeight, topGap, bottomHGap, leftWGap, nil, self.initItem);
end

-- 初始化单个子项
function UIDiary:initItem(child, index)
    local v = self.eleArr[index];

    for j = 1, 3 do
        local iconNode = findChildByName(child, "ele" .. j);
        iconNode:setOpacity(255);

        local ele;
        if j == 1 then
            ele = self.allEle[v].first;
        elseif j == 2 then
            ele = self.allEle[v].second;
        else
            ele = self.allEle[v].third;
        end

        iconNode:loadTexture(eleIcon[ele]);

        if ElementAltarM.isKeepDiary(v) ~= true then
            -- 未激活，设置半透明
            iconNode:setOpacity(128);
        end
    end

    -- 奖励说明
    local textLabel = findChildByName(child, "effect");
    textLabel:setFontName(getLocFontName());
    local applyDesc = self.allEle[v].apply_desc or "";

    -- 奖励类型图标
    local bonusIcon = findChildByName(child, "bonus_type");
    local icon      = self.allEle[v].icon;

    if ElementAltarM.isKeepDiary(v) then
        -- 已经激活，显示具体效果
        TextStyleM.setTextStyle(textLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
        textLabel:setString(applyDesc);
        bonusIcon:loadTexture(getDiaryBonusIcon(icon));
        bonusIcon:setVisible(true);
    else
        -- 尚未激活，只显示奖励类型，不显示具体效果
        TextStyleM.setTextStyle(textLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_GRAY);
        textLabel:setString(string.format(getLocStr("not_be_active")));
        bonusIcon:setVisible(false);
    end

    return child;
end

-- 适配
function UIDiary:resize()
    local w = AlignM.frameSize.width;
    local h = AlignM.frameSize.height;

    local node = self.node;

    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end
